package cn.gaotu.system.service.impl;

import cn.gaotu.basic.query.BaseQuery;
import cn.gaotu.basic.util.PageList;
import cn.gaotu.system.domain.Role;
import cn.gaotu.system.mapper.RoleMapper;
import cn.gaotu.system.service.IRoleService;
import cn.gaotu.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void save(Role role) {
        //保存role表的数据
        roleMapper.save(role);//一定要返回自增长
        //保存中间表
        initRelationTable(role);
    }

    /**
     * 处理角色关联表的数据
     * @param role
     */
    private void initRelationTable(Role role) {
        //前端必须传递最新的全量数据,我们直接做全量覆盖
        //一.删除历史数据
        roleMapper.removeRoleMenuByRoleId(role.getId());
        roleMapper.removeRolePermissionByRoleId(role.getId());
        //二.保存最新数据
        //1.保存角色权限表数据
        roleMapper.saveRoleMenu(role);
        //2.保存角色菜单表数据
        roleMapper.savePermission(role);
    }

    @Override
    public void update(Role role) {
        super.update(role);
    }

    @Override
    public void DeleteDep(Long id) {
        super.DeleteDep(id);
    }

    @Override
    public Role findById(Long id) {
        return super.findById(id);
    }

    @Override
    public List<Role> findAll() {
        return super.findAll();
    }

    @Override
    public PageList<Role> queryPage(BaseQuery baseQuery) {
        return super.queryPage(baseQuery);
    }

    @Override
    public void patchRemove(List<Long> ids) {
        super.patchRemove(ids);
    }
}
